﻿using System;
using System.Collections.Generic;
using System.Text;

using Daco.Web.UI;
using Daco.Web.Store;

namespace Daco.Web.Masters
{
    public class HOMEMaster : DACOMaster
    {
        public HOMEMaster()
        {
            this.Load += new EventHandler(HOMEMaster_Load);
        }

        protected void HOMEMaster_Load(object sender, EventArgs e)
        {
            // get the current page...
            string pageSrc = GetPageSource();

            try
            {
                Daco.Web.UI.SitePage currentPage = (Daco.Web.UI.SitePage)Page.LoadControl(pageSrc);

                CurrentPage = currentPage;
                CurrentPage.Master = this;
                CurrentPage.Header = Header;
                CurrentPage.Footer = Footer;
                CurrentPage.Navigator = Navigator;

                #region "Bind HOME Template"
                Web.UI.WebControls.CategoryTree<int, CProductCategory> categoryTree = new UI.WebControls.CategoryTree<int, CProductCategory>();
                categoryTree.Title = "Danh mục";
                categoryTree.DataSource = (IEnumerable<ICategory<int, CProductCategory>>)CProductCategory.GetCategoryTree(-1);
                categoryTree.DataBind();

                Web.UI.WebControls.ULManufacture manufactureTree = new UI.WebControls.ULManufacture();
                manufactureTree.Title = "Hãng sản xuất";
                manufactureTree.DataSource = Daco.Web.Store.CProduct.GetManufactures();
                manufactureTree.DataBind(); 
                #endregion

                Body.Left.Controls.Add(categoryTree);
                Body.Left.Controls.Add(manufactureTree);
                Body.Right.Controls.Add(CurrentPage);

                CPageContext.Current.CurrentPage = CurrentPage;
                    
                // add the header control before the page rendering...
                Controls.AddAt(0, Header);
                Controls.AddAt(1, Navigator);
                // add the page before the page rendering...
                Controls.Add(Body);
                // add the footer control after the page...
                Controls.Add(Footer);
            }
            catch (System.IO.FileNotFoundException)
            {
                // Phat sinh application exception, thong bao tai nguyen truy xuat khong ton tai hoac khong co tham quyen
                throw new ApplicationException(Daco.Web.Resources.DACO.ResourceNotFound);
            }
        }

//        protected override void Render(System.Web.UI.HtmlTextWriter writer)
//        {
//            writer.WriteLine(
//                @"<div>
//                    <div style=""margin: 0 auto; width: 1000px;"">");

//            base.Render(writer);

//            writer.WriteLine(
//                @"
//                    </div>
//                </div>
//            ");
//        }

        protected override string GetPageSource()
        {
            return base.GetPageSource();
        }
    }
}